<?php

/**
 * This is the model class for table "empresas".
 *
 * The followings are the available columns in table 'empresas':
 * @property string $codigo
 * @property string $rif
 * @property string $razon
 * @property string $nombrecomercial
 * @property string $tipoCompania
 * @property string $correo
 * @property string $domicilio
 * @property string $usuarios_login
 * @property integer $datosregistro_id
 *
 * The followings are the available model relations:
 * @property Usuarios $usuariosLogin
 * @property Datosregistro $datosregistro
 * @property Entregas[] $entregases
 * @property Obligaciones[] $obligaciones
 * @property RepJunta[] $repJuntas
 * @property RepSocios[] $repSocioses
 */
class TEmpresas extends CActiveRecord
{
	public $usuarioActual;
	public $tieneRegistro;
	/**
	 * Returns the static model of the specified AR class.
	 * @return TEmpresas the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'empresas';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('codigo, rif, razon, nombrecomercial, usuarios_login', 'required'),
			array('datosregistro_id', 'numerical', 'integerOnly'=>true),
			array('codigo', 'length', 'max'=>20),
			array('rif', 'length', 'max'=>30),
			array('rif','match','pattern'=>'/[JVE]-\d+-\d/'),
			array('razon', 'length', 'max'=>100),
			array('correo','email'),
			array('codigo','unique'),
			array('nombrecomercial', 'length', 'max'=>200),
			array('tipoCompania, correo', 'length', 'max'=>75),
			array('domicilio', 'length', 'max'=>300),
			array('usuarios_login', 'length', 'max'=>40),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('codigo, rif, razon, nombrecomercial, tipoCompania, correo, domicilio, usuarios_login, datosregistro_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'usuariosLogin' => array(self::BELONGS_TO, 'Usuarios', 'usuarios_login'),
			'datosregistro' => array(self::BELONGS_TO, 'Datosregistro', 'datosregistro_id'),
			'entregases' => array(self::HAS_MANY, 'Entregas', 'empresas_codigo'),
			'obligaciones' => array(self::HAS_MANY, 'Obligaciones', 'empresas_codigo'),
			'repJuntas' => array(self::HAS_MANY, 'RepJunta', 'empresas_codigo'),
			'repSocioses' => array(self::HAS_MANY, 'RepSocios', 'empresas_codigo'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'codigo' => 'Codigo',
			'rif' => 'Rif',
			'razon' => 'Razon Social',
			'nombrecomercial' => 'Nombre Comercial',
			'tipoCompania' => 'Tipo de Contribuyente',
			'correo' => 'Correo',
			'domicilio' => 'Domicilio Fiscal',
			'usuarios_login' => 'Asistente Asignado',
			'datosregistro_id' => 'Datos Registro',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('codigo',$this->codigo,true);
		$criteria->compare('rif',$this->rif,true);
		$criteria->compare('razon',$this->razon,true);
		$criteria->compare('nombrecomercial',$this->nombrecomercial,true);
		$criteria->compare('tipoCompania',$this->tipoCompania,true);
		$criteria->compare('correo',$this->correo,true);
		$criteria->compare('domicilio',$this->domicilio,true);
		$criteria->compare('usuarios_login',$this->usuarios_login,true);
		$criteria->compare('datosregistro_id',$this->datosregistro_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function afterFind(){
		parent::afterFind();
		$this->usuarioActual = TUtilidades::obtenerNombreUsuario($this->usuarios_login);
		$this->tieneRegistro = ($this->datosregistro_id==NULL)?false:true;
	}
}